package com.example.bzh.gamecat.httputils.hotlive;

import android.content.Context;
import android.util.Log;

import com.example.bzh.gamecat.bean.hot.BannerListBean;
import com.example.bzh.gamecat.bean.hot.DataBean;
import com.example.bzh.gamecat.bean.hot.Hotbean;
import com.example.bzh.gamecat.bean.hot.RecommendMasterListBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * Created by peter on 2016/8/18.
 */
public class HotConvertFactory extends Converter.Factory {

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
        return new HotConvert();
    }

    public class HotConvert implements Converter<ResponseBody, Hotbean> {

        @Override
        public Hotbean convert(ResponseBody value) throws IOException {
            String result = value.string();
            Log.e("TAG", "convert: " + result);
            try {
                Hotbean hotbean = getHotbean(result);
                return hotbean;
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        /*
        * userCount: 84,
        icon: "http://head.youximao.tv/208589_1466703762891",
        liveStatus: 0,
        cover: "http://img.youximao.tv//208589_1468984771954",
        masterLevel: 10,
        masterId: 208589,
        nickName: "星娱-卉卉",
        roomType: 0,
        masterNo: 1200102798,
        name: "星娱-卉卉的房间",
        roomId: 2200070250,
        pullUrl: "http://pull.youximao.tv/liveyxm/e03500faa39b76c589ca1fff34feedbb.flv"
        *
        * */

        private Hotbean getHotbean(String result) throws JSONException {
            JSONObject jsonObject = new JSONObject(result);
            JSONObject data = jsonObject.getJSONObject("data");
            JSONArray recommendMasterList = data.getJSONArray("recommendMasterList");
            List<RecommendMasterListBean> RecommendMasterList = new ArrayList<>();
            for (int i = 0; i < recommendMasterList.length(); i++) {
                JSONObject object = recommendMasterList.getJSONObject(i);
                String icon = object.getString("icon");
                int liveStatus = object.getInt("liveStatus");
                int userCount = object.getInt("userCount");
                String cover = object.getString("cover");
                int masterLevel = object.getInt("masterLevel");
                int masterId = object.getInt("masterId");
                String nickName = object.getString("nickName");
                int roomType = object.getInt("roomType");
                int masterNo = object.getInt("masterNo");
                String name = object.getString("name");
                int roomId = object.getInt("roomId");
                String pullUrl = object.getString("pullUrl");
                RecommendMasterListBean bean = new RecommendMasterListBean(userCount, liveStatus, icon, cover, masterLevel, roomType, nickName, masterId, name, masterNo, roomId, pullUrl);
                RecommendMasterList.add(bean);
            }
//            action: "http://show.youximao.tv/m/popularRank.action",
//            imgUrl: "http://head.youximao.tv/c535f535-805f-4908-a5e1-9fc7780f2ffb.png",
//            mId: 403,
//            type: 1
            JSONArray bannerList = jsonObject.getJSONArray("bannerList");
            List<BannerListBean> bannerbeanList = new ArrayList<>();
            for (int j = 0; j < bannerList.length(); j++) {
                JSONObject banerobject = bannerList.getJSONObject(j);
                String action = banerobject.getString("action");
                String imgUrl = banerobject.getString("imgUrl");
                int mId = banerobject.getInt("mId");
                int type = banerobject.getInt("type");
                BannerListBean bannerlistbean = new BannerListBean(imgUrl, mId, action, type);
                bannerbeanList.add(bannerlistbean);
            }

            DataBean dataBean = new DataBean(bannerbeanList, RecommendMasterList);

            String code = jsonObject.getString("code");
            String msg = jsonObject.getString("msg");
            Hotbean hotbean = new Hotbean(dataBean, code, msg);
            return hotbean;
        }
    }
}
